#include"UdpServe.hpp"
#include<stdlib.h>
#include"ThreadPool.hpp"

void frecv(char* buff,int socketd,sockaddr_in cli_sou){
    socklen_t len_ac=sizeof(cli_sou);
    while(true){
        std::string  lip=("client accept:");
        int m= recvfrom(socketd,buff,sizeof(buff)-1,0,(sockaddr*)&cli_sou,&len_ac);
        lip+=buff;
        if(m>0){
                std::cout<<buff<<std::endl;
        }
    }
}

void fsend(std::string line,int socketd,sockaddr_in local){
      while(1)
    {
       
        std::cout << "Please Enter# ";
        std::getline(std::cin, line);
        socklen_t len=sizeof(local);
        sendto(socketd,line.c_str(),line.size(),0,(sockaddr*)&local,len);
        std::cout<<"clint发送成功"<<std::endl;
    }
}
int main(int argc, char *argv[]){
    if(argc!=3){
       LOG(FATAL, "please input port and ip\n");
       exit(0);
    }   
   int port= atoi(argv[1]);
    std::string ip=argv[2];
    int socketd=socket(AF_INET, SOCK_DGRAM, 0);
     if (socketd < 0) {
        LOG(FATAL, "socket error\n");
        exit(SOCKET_ERROR);
    } else {
        LOG(DEBUG, "clent socket create success, _socketd: %d\n", socketd);
    }
    struct sockaddr_in local;
    memset(&local, 0, sizeof(local));
    local.sin_family = AF_INET;
    local.sin_port = htons(port);
    local.sin_addr.s_addr = inet_addr(ip.c_str());
    char buff[1024];
    struct sockaddr_in cli_sou;
    std::string line;
    func_t t=std::bind(&frecv,buff,socketd,cli_sou);
    func_t s=std::bind(&fsend,line,socketd,local);
    Thread re("get",t);
    Thread se("give",s);
    re.Start();
    se.Start();
    re.Join();
    se.Join();
    return 0;
}